#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/9/1 15:13
# @Author : wn 
# @File: orderSearch.py
# @SoftWare: PyCharm
# @Site:
# @Table:
# ｡◕‿◕｡｡◕‿◕｡｡◕‿◕｡

from django.shortcuts import render,redirect,HttpResponse
from django.http import JsonResponse
from django.views import View
from Manager.utils.mypage import my_page
from Manager.models import *
from operator import itemgetter
from Manager.utils.model_choices import *
from Manager.utils.pageSearch import *
import json




class OrderSearch(View):


    def get(self,request):
        # 当前在第几页
        off = request.GET.get('offset', None)
        # 一页几条
        lim = request.GET.get('limit', None)
        if lim is None:
            html = "orderSearch.html"
            return render(request, html, {'url': '/strategy/order_list'})
        else:
            result_list = []
            import datetime
            nowtime = datetime.datetime.now().strftime('%Y%m%d')
            # 报单查询
            or_list = list(Ruleorder.objects.filter(tradingday=nowtime))
            for item in or_list:
                result_dict = {}
                result_dict['Tradingday'] = item.tradingday
                result_dict['RuleID'] = item.ruleid
                result_dict['OrderInnerID'] = item.orderinnerid
                result_dict['OrderSysID'] = item.ordersysid
                result_dict['AccountID'] = item.accountid
                result_dict['InstrumentID'] = item.instrumentid
                result_dict['InstrumentName'] = item.instrumentname
                result_dict['Direction'] = DirectionChoices.get(item.direction,None)
                result_dict['OffSetFlag'] = OffsetFlagChoices.get(item.offsetflag,None)
                result_dict['OrderStatus'] = OrderStatusChoices.get(item.orderstatus,None)
                result_dict['Price'] = item.price
                result_dict['VolumeTotalOriginal'] = item.volume
                result_dict['VolumeTotal'] = item.volumetotal
                result_dict['VolumeTraded'] = item.volumetraded
                result_dict['OrderStatusMsg'] = item.statusmsg
                result_dict['InsertTime'] = item.inserttime
                result_dict['UpdateTime'] = item.updatetime
                result_dict['AssetNo'] = item.assetno
                result_dict['CombiNo'] = item.combino
                result_dict['QuoteType'] = QuoteTypeChoices.get(item.quotetype,None)
                result_dict['CommodityType'] = CommodityTypeChoices.get(item.commoditytype,None)
                result_dict['BilateralID'] = item.bilateralid
                result_list.append(result_dict)
            # 排序
            for item in result_list:
                if (item.get('tradingday') is None):
                    item['tradingday'] = '-1'
            result_list_sorted = sorted(result_list, key=itemgetter('tradingday'), reverse=True)
            result = my_page(result_list_sorted, lim, off)
            return JsonResponse(result, safe=False)



    def post(self,request):
        if request.POST.get('orderSearch_select', None):
            data = json.loads(request.POST.get('data'))
            Tradingday = data.get('Tradingday',None)
            RuleID = data.get('RuleID',None)
            OrderInnerID = data.get('OrderInnerID',None)
            AccountID = data.get('AccountID',None)
            InstrumentID = data.get('InstrumentID',None)
            OrderStatus = data.get('OrderStatus',None)
            jsonRes = search_ordersearch(Tradingday=Tradingday,RuleID=RuleID,OrderInnerID=OrderInnerID,AccountID=AccountID,InstrumentID=InstrumentID,OrderStatus=OrderStatus)
            return jsonRes
